/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.List;
import java.util.ArrayList;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.swt.graphics.Color;

import edu.pku.notation.runtime.draw2d.TextFigure;
import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.edit.policies.StructuredLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.NestedNodeLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.commands.CreateLinkCommand;
import edu.pku.sei.modeler.images.ImageProvider;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.MetaLocationManager;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.Nodes.Device;
import edu.pku.sei.modeler.figures.NodeFigure;
import edu.pku.sei.modeler.model.uml.BasicActivities.ObjectFlow;
import edu.pku.sei.modeler.model.uml.BasicActivities.ControlFlow;
import edu.pku.sei.modeler.model.uml.BasicActivities.ActivityEdge;
import edu.pku.sei.modeler.model.uml.UseCases.Include;
import edu.pku.sei.modeler.model.uml.UseCases.Extend;
import edu.pku.sei.modeler.model.uml.Interfaces.InterfaceRealization;
import edu.pku.sei.modeler.model.uml.Dependencies.Realization;
import edu.pku.sei.modeler.model.uml.Dependencies.Usage;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.Kernel.PackageMerge;
import edu.pku.sei.modeler.model.uml.Kernel.Association;
import edu.pku.sei.modeler.model.uml.BasicInteractions.GeneralOrdering;
import edu.pku.sei.modeler.model.uml.Kernel.Generalization;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Message;
import edu.pku.sei.modeler.model.uml.Kernel.PackageImport;
import edu.pku.sei.modeler.uml.ResourceFactory;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;


/**
 * @generated
 *
 */
public class DeviceEditpart extends AbstractUMLNodeEditPart 
{
	public DeviceEditpart (MetaShapeElement model) {
		super(model);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		Label header = new Label("<<Device>>");
		header.setFont(ResourceFactory.getTextFont());
		header.setForegroundColor(ColorConstants.blue);
		NodeFigure figure = new NodeFigure(header);
		return figure;
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {
			protected Command getDirectEditCommand(DirectEditRequest request) {
				return new ChangeNameCommand(getDevice(),
						(String) request.getCellEditor().getValue());
			}

			protected void showCurrentEditValue(DirectEditRequest request) {
				IFigure fig = getDirectEditFigure();
				if (fig instanceof Label) {
					((Label) fig).setText(getDevice().getName());
					fig.getUpdateManager().performUpdate();
				}
			}
		};
	}
	
	@Override
	protected IFigure getDirectEditFigure() {
		return ((NodeFigure) this.getFigure()).getNodeLabel();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new NestedNodeLayoutEditPolicy(((MetaNode)this.getModel()).getDiagram().getType()));
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		super.handlePropertyChanged(event);
		refreshVisuals();
	}
	
	private Device getDevice() {
		return (Device)((MetaNode)getModel()).getModelElement();
	}
	
	protected void refreshVisuals() {
		super.refreshVisuals();
		Device modelEle = (Device)getNode().getModelElement();
		NodeFigure figure = (NodeFigure)getFigure();
		
		figure.getNodeLabel().setText(""+modelEle.getName());
		
	}
	
	@Override
	public IFigure getContentPane() {
		return ((NodeFigure)getFigure()).getBody();
	}
}
